/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package cn.yzc.config;

import cn.yzc.interceptor.LoginInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

/**
 * <p>Project: SpringMVCPro - WebMvcConfig</p>
 * <p>Powered by yzc On 2023-11-03 14:20:41</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Configuration
@EnableWebMvc
@ComponentScan("cn.yzc")
@MapperScan("cn.yzc.mapper")
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 编码
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter =new StringHttpMessageConverter();
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN));
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        converters.add(converter);
        //添加MappingJackson2HttpMessageConverter用于处理JSON数据
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        jsonConverter.setDefaultCharset(StandardCharsets.UTF_8);
        converters.add(jsonConverter);
        WebMvcConfigurer.super.configureMessageConverters(converters);
    }

    /**
     * 注入文件上传解析器  要求方名称必须是multipartResolver
     * @return
     */
    @Bean
    public MultipartResolver multipartResolver(){
        return new StandardServletMultipartResolver();
    }

    /**
     * 注册拦截器
     * @param registry
     */
   /* @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册登录拦截器
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/login");
        WebMvcConfigurer.super.addInterceptors(registry);
    }*/

    /**
     * 类型转换器
     * @param
     */

/* @Override
    public void addFormatters(FormatterRegistry registry) {
        //实现Converter接口的转换器
        registry.addConverter(new PointConvert());
        WebMvcConfigurer.super.addFormatters(registry);
    }*/

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        //这种方式 会拦截静态资源 但允许将没有映射的资源逃到默认的default servlet处理
        configurer.enable();
        WebMvcConfigurer.super.configureDefaultServletHandling(configurer);
    }

}
